/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import com.qq.spirit.inter.AbstractXmlActionAfterRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.Pair;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * 纺织任务
 * 
 * @author beedoorwei
 * 
 */
public class MakerTask extends TaskUnit
{
	static List<Integer> bmakeList = new ArrayList<Integer>();
	static Map<String, Integer> makerMap = new HashMap<String, Integer>();
	static
	{
		bmakeList.add(82);
		bmakeList.add(78);
		bmakeList.add(74);
		bmakeList.add(70);
		bmakeList.add(66);
		bmakeList.add(62);
		bmakeList.add(58);
		bmakeList.add(54);
		bmakeList.add(50);
		bmakeList.add(54);
		bmakeList.add(40);
		bmakeList.add(36);
		bmakeList.add(32);
		bmakeList.add(28);

		// makerMap.put("76", "62,
		// makerMap.put("75", "32");
		// makerMap.put("74", "101");
		// makerMap.put("73", "81");
		// makerMap.put("72", "61");
		// makerMap.put("71", "31");
		// makerMap.put("70", "100");
		// makerMap.put("69", "80");
		// makerMap.put("68", "60");
		// makerMap.put("67", "30");
		// makerMap.put("66", "99");
		// makerMap.put("65", "79");
		// makerMap.put("64", "59");
		// makerMap.put("63", "29");
		// makerMap.put("62", "98");
		// makerMap.put("61", "78");
		// makerMap.put("60", "58");
		// makerMap.put("59", "28");
		// makerMap.put("58", "97");
		// makerMap.put("57", "77");
		// makerMap.put("56", "57");
		// makerMap.put("55", "27");
		// makerMap.put("54", "96");
		// makerMap.put("53", "76");
		// makerMap.put("52", "56");
		// makerMap.put("51", "26");
		// makerMap.put("50", "95");
		// makerMap.put("49", "75");
		// makerMap.put("48", "55");
		// makerMap.put("47", "25");
		// makerMap.put("46", "54");
		// makerMap.put("45", "24");
		// makerMap.put("44", "53");
		// makerMap.put("43", "23");
		// makerMap.put("42", "94");
		// makerMap.put("41", "74");
		// makerMap.put("40", "52");
		// makerMap.put("39", "22");
		// makerMap.put("38", "51");
		// makerMap.put("37", "21");
		// makerMap.put("36", "20");
		// makerMap.put("35", "50");
		// makerMap.put("34", "19");
		// makerMap.put("33", "18");
		// makerMap.put("32", "93");
		// makerMap.put("31", "73");
		// makerMap.put("30", "49");
		// makerMap.put("29", "17");
		// makerMap.put("28", "48");
		// makerMap.put("27", "16");
		// makerMap.put("26", "15");
		// makerMap.put("25", "47");
		// makerMap.put("24", "14");
		// makerMap.put("23", "13");
		// makerMap.put("22", "92");
		// makerMap.put("21", "72");
		// makerMap.put("20", "46");
		// makerMap.put("19", "12");
		// makerMap.put("18", "45");
		// makerMap.put("17", "11");
		// makerMap.put("16", "10");
		// makerMap.put("15", "44");
		// makerMap.put("14", "9");
		// makerMap.put("13", "8");
		// makerMap.put("12", "91");
		// makerMap.put("11", "71");
		// makerMap.put("10", "43");
		// makerMap.put("9", "7");
		// makerMap.put("8", "42");
		// makerMap.put("7", "6");
		// makerMap.put("6", "41");
		// makerMap.put("5", "5");
		// makerMap.put("4", "4");
		// makerMap.put("3", "3");
		// makerMap.put("2", "2");
		// makerMap.put("1", "1");

		makerMap.put("53", 15);
		makerMap.put("33", 14);
		makerMap.put("23", 13);
		makerMap.put("13", 12);
		makerMap.put("52", 11);
		makerMap.put("32", 10);
		makerMap.put("22", 9);
		makerMap.put("12", 8);
		makerMap.put("51", 7);
		makerMap.put("31", 6);
		makerMap.put("21", 5);
		makerMap.put("11", 4);
		makerMap.put("43", 3);
		makerMap.put("42", 2);
		makerMap.put("41", 1);
	}

	public MakerTask()
	{
		this.taskDesc = "纺织任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			if (!GameUserInfoUtil.getIsUserLoadSuc())
			{
				TaskManager.addTask(this, 10);
			} else
			{
				Iterator<Integer> ite = GameUserInfoUtil.userIte();
				while (ite.hasNext())
				{
					int gid = ite.next();
					UserData u = GameUserInfoUtil.getUser(gid);
					if (u.getConfigValue("openMaker", "0").equals("1"))
					{
						TaskManager.addTask(new Maker(gid), GameUtil.randomInt(10));
					}
				}
			}
		} catch (Exception e)
		{
			LoggerUtil.error(gid, e);
		}
	}

	/**
	 * 纺织任务
	 * 
	 * @author Administrator
	 * 
	 */
	class Maker extends TaskUnit
	{
		public Maker(int gid)
		{
			this.gid = gid;
		}

		public void doTask()
		{
			try
			{
				UserData u = GameUserInfoUtil.getUser(gid);
				if (u.getUserState() == UserState.LOGIN_GAME)
				{
					// 获取纺织信息
					HttpUtil.doGet(gid, GameConstants.MAKE_QUERY_URL, new PreMakeAction(gid));
				} else
				{
					// 添加登录任务先
					TaskManager.addTask(new LoginTask(gid, this));
				}
			} catch (Exception e)
			{
				LoggerUtil.error(gid, e);
			}
		}
	}

	/**
	 * 征战前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class PreMakeAction extends AbstractXmlActionAfterRequest
	{
		public PreMakeAction(int gid)
		{
			super();
			this.gid = gid;
			this.taskUnit = new Maker(gid);
		}

		public void doCall(List<Header> headerList, XmlNodeData battleMultiInfo)
		{
			doMaker(battleMultiInfo);
		}

		/**
		 * 开始纺织
		 */
		private void doMaker(XmlNodeData battleMultiInfo)
		{
			String makenum = XmlDataUtil.getNodeData(battleMultiInfo, "results.makenum");
			String lv = XmlDataUtil.getNodeData(battleMultiInfo, "results.lv");
			String tech = XmlDataUtil.getNodeData(battleMultiInfo, "results.tech");
			if (GameUtil.convertInt(makenum) > 0)
			{
				// cd在新版本中取消，那么开始搞呗
				HttpUtil.doGet(gid, GameConstants.MAKE_WEAVER_ASS_URL, new MakerWeaverAction(gid, lv, tech));
			}

		}
	}

	/**
	 * 发起攻击事后处理对象
	 * 
	 * @author Administrator
	 * 
	 */
	class MakerWeaverAction extends AbstractXmlActionAfterRequest
	{
		String lv = null;
		String tech = null;

		public MakerWeaverAction(int gid, String lv, String tech)
		{
			super();
			this.lv = lv;
			this.gid = gid;
			this.tech = tech;
			this.taskUnit = new Maker(gid);
		}

		@Override
		public void doCall(List<Header> headerList, XmlNodeData battleMultiAttackInfo)
		{
			// 纺织策略分3种，等级优先，科技优先，以及纺织固定布r
			List<XmlNodeData> xlist = XmlDataUtil.getNodeInfoList(battleMultiAttackInfo, "results.assplayer.weaverlevel");
			UserData u = GameUserInfoUtil.getUser(gid);
			String waverType = u.getConfigValue("waverType", "");
			Pair<String, Pair<String, String>> waverHelper = null;
			if (waverType.equals("1"))
			{
				// 纺织固定等级的布
				if (GameUtil.convertInt(lv) >= GameUtil.convertInt(u.getConfigValue("waverLevel", "")))
				{
					waverHelper = getFixLevelWeaver(u.getConfigValue("waverLevel", ""), xlist);
				} else
				{
					waverHelper = getLevelFirstWeaver(xlist);
				}
			} else if (waverType.equals("2"))
			{
				// 科技优先
				waverHelper = getTechFirstWeaver(xlist);

			} else if (waverType.equals("3"))
			{
				// 等级优先
				getLevelFirstWeaver(xlist);
			}

			// 开始纺织吧
			List<BasicNameValuePair> nvpList = new ArrayList<BasicNameValuePair>();
			nvpList.add(new BasicNameValuePair("firstAssPId", waverHelper.second.first));
			nvpList.add(new BasicNameValuePair("secondAssPId", waverHelper.second.second));
			nvpList.add(new BasicNameValuePair("makeId", waverHelper.first));
			HttpUtil.doPost(gid, GameConstants.MAKE_DO_WEAVER_ASS_URL, nvpList, new MakerAction(gid));
		}

		// 按固定等级布进行织布
		private Pair<String, Pair<String, String>> getFixLevelWeaver(String lv, List<XmlNodeData> xlist)
		{
			UserData u = GameUserInfoUtil.getUser(gid);
			String waverLv = u.getConfigValue("waverLevel", "");
			List<XmlNodeData> slist = new ArrayList<XmlNodeData>();
			for (XmlNodeData xv : xlist)
			{
				String helperTech = XmlDataUtil.getNodeData(xv.getParent(), "tech");
				if (tech.charAt(0) == helperTech.charAt(0))
				{
					continue;
				}
				slist.add(xv);
			}
			Collections.sort(slist, new MakerComparator());

			return Pair.makePair(waverLv, Pair.makePair(XmlDataUtil.getNodeData(slist.get(0).getParent(), "playerid"), XmlDataUtil.getNodeData(slist.get(1).getParent(), "playerid")));
		}

		// 按科技优先级进行织布
		private Pair<String, Pair<String, String>> getTechFirstWeaver(List<XmlNodeData> xlist)
		{
			List<XmlNodeData> slist = new ArrayList<XmlNodeData>();
			for (XmlNodeData xv : xlist)
			{
				String helperTech = XmlDataUtil.getNodeData(xv.getParent(), "tech");
				if (tech.charAt(0) == helperTech.charAt(0))
				{
					continue;
				}
				slist.add(xv);
			}
			Collections.sort(slist, new MakerComparator());

			XmlNodeData x1 = slist.get(0);
			XmlNodeData x2 = slist.get(1);
			int s1Level = GameUtil.convertInt(XmlDataUtil.getNodeData(x1.getParent(), "weaverlevel"), 0);
			int s2Level = GameUtil.convertInt(XmlDataUtil.getNodeData(x2.getParent(), "weaverlevel"), 0);
			int maxLv = 0;
			int[] lvArray = new int[] { s1Level, s2Level, GameUtil.convertInt(lv, 0) };
			for (int i = 0; i < lvArray.length; i++)
			{
				if (maxLv < lvArray[i])
				{
					maxLv = lvArray[i];
				}
			}
			int canLv = 0;
			for (Integer i : bmakeList)
			{
				if (maxLv > i)
				{
					canLv = i;
					break;
				}
			}

			return Pair.makePair(canLv + "", Pair.makePair(XmlDataUtil.getNodeData(x1.getParent(), "playerid"), XmlDataUtil.getNodeData(x2.getParent(), "playerid")));
		}

		private Pair<String, Pair<String, String>> getLevelFirstWeaver(List<XmlNodeData> xlist)
		{
			UserData u = GameUserInfoUtil.getUser(gid);
			List<XmlNodeData> slist = new ArrayList<XmlNodeData>();
			XmlNodeData xMaxNode = null;
			int maxLev = 0;
			for (XmlNodeData xv : xlist)
			{
				if (xMaxNode == null)
				{
					// 开始纺织吧
					xMaxNode = xv;
					maxLev = GameUtil.convertInt(XmlDataUtil.getNodeData(xv.getParent(), "weaverlevel"));
					continue;
				}

				int weaverlevel = GameUtil.convertInt(XmlDataUtil.getNodeData(xv.getParent(), "weaverlevel"));
				if (maxLev < weaverlevel)
				{
					maxLev = weaverlevel;
					xMaxNode = xv;
				}
			}
			if (GameUtil.convertInt(lv, 0) > maxLev)
			{
				// 自身最大，寻找科技优先吧
				return getTechFirstWeaver(xlist);
			} else
			{
				// 排除最大的
				xlist.remove(xMaxNode);
				Collections.sort(slist, new MakerComparator());
				XmlNodeData x1 = slist.get(0);
				int canLv = 0;
				for (Integer i : bmakeList)
				{
					if (maxLev > i)
					{
						canLv = i;
						break;
					}
				}

				return Pair.makePair(canLv + "", Pair.makePair(XmlDataUtil.getNodeData(xMaxNode.getParent(), "playerid"), XmlDataUtil.getNodeData(x1.getParent(), "playerid")));
			}
		}

	}

	/**
	 * 纺织操作回调事件
	 * 
	 * @author Administrator
	 * 
	 */
	class MakerAction extends AbstractXmlActionAfterRequest
	{
		public MakerAction(int gid)
		{
			super();
			this.gid = gid;
			this.taskUnit = new Maker(gid);
		}

		@Override
		public void doCall(List<Header> headerList, XmlNodeData battleMultiInfo)
		{
			String copper = XmlDataUtil.getNodeData(battleMultiInfo, "results.playerupdateinfo.copper");
			String profit = XmlDataUtil.getNodeData(battleMultiInfo, "results.playerupdateinfo.makeresult.makeresult.profit");
			LoggerUtil.info(gid, " 纺织获得银币：" + profit + "，当前银币数目为:" + copper);
			// 提交5秒钟后刷新任务,如果成功刷新等待开始攻击，否则重新征战任务
			TaskManager.addTask(new Maker(gid), GameUtil.randomInt(10));
		}
	}

	class MakerComparator implements Comparator<XmlNodeData>
	{

		@Override
		public int compare(XmlNodeData o1, XmlNodeData o2)
		{
			String o1tech = XmlDataUtil.getNodeData(o1.getParent(), "tech");
			String o2tech = XmlDataUtil.getNodeData(o2.getParent(), "tech");
			int o1Weight = makerMap.get(o1tech);
			int o2Weight = makerMap.get(o2tech);
			if (o1Weight > o2Weight)
			{
				return -1;
			}
			return 1;
		}
	}
}